<?php
/*
	
*/
defined('BY_JYA') or exit('Access Denied');
global $_RQ,$_AR;

$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
$return['diy']=$this->plugin_setting('diy');
$basic=$this->plugin_setting('basic');
if($_RQ['op']=='main'){
	$id = intval($_RQ['id']);
	$fromuser = intval($_RQ['fromuser']);
	$this->session('fromuser',$fromuser);
	$action='jy_quan/item//id='.$id;
	$this->add_member($id,$fromuser,'','','',$action);
	if(!$id){
		exi('没有找到该帖子，请重新选择！','error');
	}
	$item=pdo_get('jy_quan_item',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	$user = pdo_get('jy_quan_member',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$item['mid']));
	$member = pdo_get('core_members',array('id'=>$item['mid'],'uniacid'=>$_SESSION['uniacid']),array('createtime','credit1','credit2','mobile','avatar','nickname'));
	//添加一次浏览次数
	pdo_update('jy_quan_item',array('looks +='=>1),array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	//进入详情，记录访问者浏览信息
	$this->model('model')->upitemlook($id,$fromuser);
	$item['fromusernum']=pdo_count('jy_quan_looklog',array('uniacid'=>$_SESSION['uniacid'],'pid'=>$item['id'],'fromuser'=>$_SESSION['uid']),'id');
	
	$item['jielinum']=pdo_count('jy_quan_item_jieli',array('uniacid'=>$_SESSION['uniacid'],'pid'=>$item['id']),'id');
	$item['msgnum']=pdo_count('jy_quan_item_msg',array('checked'=>1,'uniacid'=>$_SESSION['uniacid'],'pid'=>$item['id']),'id');
	$item['jieli']=pdo_getall('jy_quan_item_jieli',array('uniacid'=>$_SESSION['uniacid'],'pid'=>$item['id']),'','','createtime desc');
	$item['isjielong']=pdo_get('jy_quan_item_jieli',array('uniacid'=>$_SESSION['uniacid'],'pid'=>$item['id'],'mid'=>$_SESSION['uid']),'id');
	$item['isjielong'] = $item['isjielong']?1:0;
	if($basic['is_jiemore'] && ($item['type']==0 || $item['type']==3)){
		$item['isjielong']=0;
	}
	//我带来的
	$item['mygoodsum'] = pdo_count('jy_quan_item_jieli',array('uniacid'=>$_SESSION['uniacid'],'pid'=>$item['id'],'fromuser'=>$_SESSION['uid']),'id');
	$item['isgetred']=pdo_getcolumn('jy_quan_redlog',array('pid'=>$item['id'],'uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'status'=>1),'id');

	//按点赞数排序作品
	$pindex = max(1, intval($_RQ['page']));
	$psize = 30;
	$item['msg']=pdo_getslice('jy_quan_item_msg',array('checked'=>1,'uniacid'=>$_SESSION['uniacid'],'pid'=>$item['id'],'cid'=>0),array($pindex,$psize),$total,$fields,'','createtime desc');
	if($item['msg']){
		foreach($item['msg'] as &$m){
			$m['createtime'] = date('Y-m-d H:i',$m['createtime']);
			$m['floorid'] = '';
			$m['time'] = date('m/d',$m['createtime']);
			$m['msglike'] = pdo_getall('jy_quan_item_msglike',array('uniacid'=>$_SESSION['uniacid'],'pid'=>$item['id'],'cid'=>$m['id']));
			if($m['msglike']){
				foreach($m['msglike'] as &$lv){
					$lv['member'] = pdo_get('core_members',array('id'=>$lv['mid'],'uniacid'=>$_SESSION['uniacid']),array('avatar','nickname'));
				}
			}
			$m['msg'] = pdo_getall('jy_quan_item_msg',array('checked'=>1,'uniacid'=>$_SESSION['uniacid'],'pid'=>$item['id'],'cid'=>$m['id']),'','createtime desc');
			if($m['msg']){
				foreach($m['msg'] as &$mv){
					$mv['member'] = pdo_get('core_members',array('id'=>$mv['mid'],'uniacid'=>$_SESSION['uniacid']),array('avatar','nickname'));
				}
			}
			$m['member'] = pdo_get('core_members',array('id'=>$m['mid'],'uniacid'=>$_SESSION['uniacid']),array('credit1','credit2','mobile','avatar','nickname'));
		}
	}
	if($item['jieli']){
		foreach($item['jieli'] as $k=>&$j){
			$j['number'] = count($item['jieli']) -$k;
			$j['createtime'] = date('Y-m-d H:i',$j['createtime']);
			$j['member'] = pdo_get('core_members',array('id'=>$j['mid'],'uniacid'=>$_SESSION['uniacid']),array('credit1','credit2','mobile','avatar','nickname'));
			if($j['member'] && $item['fansinfo_set']==1){
				$j['member']['nickname']='********';
			}
		}
	}
	$psize = 12;
	$item['looksmember']=pdo_getslice('jy_quan_looklog',array('uniacid'=>$_SESSION['uniacid'],'pid'=>$item['id']),array($pindex,$psize),$total,array('mid'),'','createtime desc');
	foreach($item['looksmember'] as &$lv){
		$lv['avatar']=pdo_getcolumn('core_members',array('id'=>$lv['mid'],'uniacid'=>$_SESSION['uniacid']),'avatar');
	}
	$item['islike'] = pdo_count('jy_quan_item_like',array('uniacid'=>$_SESSION['uniacid'],'pid'=>$item['id'],'mid'=>$_SESSION['uid']));
	$item['likes'] = pdo_getall('jy_quan_item_like',array('uniacid'=>$_SESSION['uniacid'],'pid'=>$item['id']));
	if($item['likes']){
		foreach($item['likes'] as &$liv){
			$liv['member'] = pdo_get('core_members',array('id'=>$liv['mid'],'uniacid'=>$_SESSION['uniacid']),array('avatar','nickname'));
		}
	}
	$return['typetitle']=$types[$item['type']];
	$return['isme']=$item['mid']==$_SESSION['uid'] ? 1 :0;
	$item['onlinetime']=intval($item['onlinetime'])?$item['onlinetime']:$item['createtime']+7*24*3600;
	$item['endstatus']=$item['onlinetime']>time()?1:0;
	$item['endtime']=$item['onlinetime'];
	$item['nowtime']=time();
	$item['onlinetime2']=$this->model()->date($item['onlinetime']-TIMESTAMP);
	$item['onlinetime']=date('Y-m-d H:i:s',$item['onlinetime']);
	$item['createtime']=$item['createtime']?date('Y-m-d H:i:s',$item['createtime']):'';
	$item['content'] = json_decode($item['content'],true);
	if($item['content']){
		foreach($item['content'] as &$row){
			if($row['type']==1){
				if($_SESSION['wxapp_id'] && !$_SESSION['fromh5wxapp']){
					$item['desc'] = $row['data'];
				}else{
					$item['desc'] = str_replace("\n","</br>",$row['data']);
				}
				$row['data'] = $item['desc'];
			}
			if($row['type']==10){
				$item['image'] = tomedia($row['data'][0],true);
			}
			if($row['type']==3){
				$item['image'] = tomedia($row['data'],true);
			}
		}
	}
	$item['fileds'] = json_decode($item['fileds'],true);
	$item['fee']=0;
	if($item['fileds']){
		$totalprice=0;
		foreach($item['fileds'] as $k=>&$fv){
			if($item['type']==1){
				$item['morefileds']=array();
				if(count($fv)>5){
					foreach($fv as $km=>&$mv){
						if($km>4){
							$item['morefileds'][]=$mv;
						}
					}
				}
				$fv['title'] = $fv[0]['data']?$fv[0]['data']:'商品名称';
				$fv['image'] = $fv[1]['data'];
				$fv['unit'] = $fv[2]['data']?$fv[2]['data']:'个';
				if($fv['options']){
					$fv['price']=$fv['options'][0]['price'];
					$fv['optionid']=0;
				}else{
					$fv['price'] = $fv[3]['data']?$fv[3]['data']:'0.00';
				}
				$fv['kucun'] = $fv[4]['data']?$fv[4]['data']:0;
				$fv['total'] = 0;
				//$totalprice +=$fv['price'];
				$item['image']=tomedia($fv['image'],true);
			}
			if($item['type']==5){
				$item['mypingxuan'] = pdo_getcolumn('jy_quan_item_jieli',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'pid'=>$id),'fileds');
				$item['mypingxuan'] = json_decode($item['mypingxuan'],true);
				$fv['selected'] = $item['mypingxuan'][$k]['selected'];
				if($fv['selected']==1){
					$fv['member'] = pdo_getall('core_members',array('id'=>$j['mid'],'uniacid'=>$_SESSION['uniacid']),array('avatar','nickname'));
				}
				$fv['jieli'] = $item['jieli'];
				foreach($fv['jieli'] as $k=>$v){
					if($fv['selected']!=1){
						unset($fv['jieli'][$k]);
					}
				}
			}
			//报名数据
			if($item['type']==3){
				$fv['total'] = 1;
				$fv['title'] = $fv[0]['data']?$fv[0]['data']:'项目名称';
				$fv['price'] = $fv[1]['data']?$fv[1]['data']:'0.00';
				$fv['num'] = $fv[2]['data']?$fv[2]['data']:'报名总人数';
				$totalprice +=$fv['price'];
			}
			foreach($fv as &$f){
				if($f['type']==18){
					$item['fee'] +=$f['data'];
				}
			}
		}
	}
	//初始化fileds：
	$fileds = ijson_encode($item['fileds']);
	$this->session('fileds',$fileds);
	$return['totalprice']=$totalprice;
	$item['diyform'] = json_decode($item['diyform'],true);
	$item['groupbuyfloor'] = json_decode($item['groupbuyfloor'],true);
	if(empty($item['groupbuyfloor'])){
		$item['groupbuyfloor'][0]=array('totle'=>1,'zhekou'=>10);
	}
	$item['desc'] = $item['desc']?$item['desc']:$user['desc'];
	//小程序码
	$image=ROOT_AT.'images/jy_quan/'.$_SESSION['uniacid'].'/id'.$id.'.jpg';
	if(!file_exists($image)){
		$url='/jy_quan/item/index?id='.$id.'&fromuser='.$_SESSION['uid'];
		$wxapp=pdo_get('core_account_wxapp',array('id'=>$_SESSION['wxapp_id'],'uniacid'=>$_SESSION['uniacid']));
		$res=model('core/wxapp')->appcode($url,$wxapp['appid'],$wxapp['appsecret'],200);
		$path=ROOT_AT.'images/jy_quan/'.$_SESSION['uniacid'];
		if(!file_exists($path)){
				mkdir($path,0700, true);
		}
		if($res){
			file_put_contents($path.'/id'.$id.'.jpg',$res);
		}
		
	}
	$item['appcode'] = 'images/jy_quan/'.$_SESSION['uniacid'].'/id'.$id.'.jpg';
	$item['goods']=pdo_get('jy_weishop_goods',array('id'=>$item['goodsid'],'uniacid'=>$_SESSION['uniacid']),array('id','title','subtitle','content','image','price','salenum','unit','merchid'));
	$item['goods']['merchname']=pdo_getcolumn('jy_merch_user',array('id'=>$item['goods']['merchid'],'uniacid'=>$_SESSION['uniacid']),'merchname');
	$this->session('id',$_RQ['id']);
	$return['user']=$user;
	$member['time']=date('Y-m-d H:i:s',time());
	$return['member']=$member;
	$return['item']=$item;
	$return['post']=$post;
	$return['islike']=$islook['islike'];
	//$return['isfollow']=pdo_getcolumn('jy_quan_item_share',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'userid'=>$item['mid']),'isfollow');
	$link=custom_url('jy_quan/item//id='.$id.'&fromuser='.$_SESSION['uid'].'&current_plugin='.$_SESSION['current_plugin'],true);
	if($item['share_set']==2 && !$return['isme']){
        $share_title='只有发布者可以转发分享！';
        $share_desc='本次给你分享到平台首页！';
        $link=custom_url('jy_quan/index//fromuser='.$_SESSION['uid'].'&current_plugin='.$_SESSION['current_plugin'],true);
    }
    if($item['status']!=1 || $return['noshow']){
        if($item['mid']!=$_SESSION['uid']){
            exi('帖子待审核中!','error','jy_quan/index/');
        }
        $share_title='帖子待审核中，不弄转发！';
        $share_desc='本次给你分享到平台首页！';
        $link=custom_url('jy_quan/index//fromuser='.$_SESSION['uid'].'&current_plugin='.$_SESSION['current_plugin'],true);
    }
	$return['sharedata']=array(
		'title'=>$share_title?:$item['title'],
		'desc'=>$share_desc?:$item['desc'],
		'link'=>$link,
		'imgUrl'=>$member['avatar'],
	);
	//小程序要用
	$return['uid']=$_SESSION['uid'];
	set_html('title',$item['title']);
	exi($return);
}
if($_RQ['op']=='follow'){
	$id = $this->session('id');
	if(!$id){
		exi('没有找到该作品，请重新选择！','error');
	}
	$item=pdo_get('jy_quan_item',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	$follow=pdo_get('jy_quan_userlog',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'userid'=>$item['mid']));
	if($follow){
		if($follow['isfollow'] !=1){
			pdo_update('jy_quan_userlog',array('isfollow'=>1,'createtime'=>TIMESTAMP),array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'userid'=>$item['mid']));
			
		}else{
			pdo_update('jy_quan_userlog',array('isfollow'=>0),array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'userid'=>$item['mid']));
		}
	}else{
		$insert=array(
			'uniacid'=>$_SESSION['uniacid'],
			'userid'=>$item['mid'],
			'mid'=>$_SESSION['uid'],
			'isfollow'=>1,
			'createtime'=>TIMESTAMP
		);
		pdo_insert('jy_quan_userlog',$insert);
	}
	exi('关注成功');
}
if($_RQ['op']=='pingxuan'){
	$id = $this->session('id');
	$postdata=get_postdata();
	$index = intval($postdata['index']);
	if(!$id){
		exi('没有找到该作品，请重新选择！','error');
	}
	$item=pdo_get('jy_quan_item',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	$item['fileds'] = json_decode($item['fileds'],true);
	$fileds=pdo_getcolumn('jy_quan_item_jieli',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'pid'=>$id),'fileds');
	if($fileds){
		$fileds=json_decode($fileds,true);
		if($fileds[$index]['selected']==1){
			$fileds[$index]['selected']=0;
			$item['fileds'][$index]['nums'] -=1;
		}else{
			$fileds[$index]['selected']=1;
			$item['fileds'][$index]['nums'] +=1;
		}		
		pdo_update('jy_quan_item_jieli',array('fileds'=>ijson_encode($fileds)),array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'pid'=>$id));
	}else{
		$fileds = $item['fileds'];
		$fileds[$index]['selected']=1;
		$insert=array(
					'uniacid'=>$_SESSION['uniacid'],
					'mid'=>$_SESSION['uid'],
					'pid'=>$id,
					'snsid'=>$item['snsid'],
					'type'=>$item['type'],
					'orderid'=>0,
					'fileds'=>ijson_encode($item['fileds']),
					'createtime'=>TIMESTAMP
		);
		pdo_insert('jy_quan_item_jieli',$insert);
		$item['fileds'][$index]['nums'] +=1;
	}
	pdo_update('jy_quan_item',array('fileds'=>ijson_encode($item['fileds'])),array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	exi('接龙成功');
}
if($_RQ['op']=='shoucang'){
	$id = $this->session('id');
	if(!$id){
		exi('没有找到该作品，请重新选择！','error');
	}
	$item=pdo_get('jy_quan_item',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	$shoucang=pdo_get('jy_quan_shoucang',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'pid'=>$id));
	if($shoucang){
		if($shoucang['isshoucang'] !=1){
			pdo_update('jy_quan_shoucang',array('isshoucang'=>1,'createtime'=>TIMESTAMP),array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'pid'=>$id));
			exi('收藏成功');
		}else{
			pdo_update('jy_quan_shoucang',array('isshoucang'=>0),array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'pid'=>$id));
		   exi('取消收藏');
		}
	}else{
		$insert=array(
			'uniacid'=>$_SESSION['uniacid'],
			'pid'=>$id,
			'mid'=>$_SESSION['uid'],
			'isshoucang'=>1,
			'createtime'=>TIMESTAMP
		);
		pdo_insert('jy_quan_shoucang',$insert);
	}
	exi('收藏成功');
}
if($_RQ['op']=='setmsg'){
	$id = $this->session('id');
	$item=pdo_get('jy_quan_item',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	$postdata=get_postdata();
	$checked = ($basic['msg_shenhe'] || $basic['open_wxappsh'])?0:1;
	$data=array(
		'title'=>trim($postdata['msg']),
		'images'=>ijson_encode($postdata['images']),
		'mid'=>$_SESSION['uid'],
		'pid'=>$id,
		'type'=>$item['type'],
		'cid'=>intval($postdata['cid']),
		'uniacid'=>$_SESSION['uniacid'],
		'checked'=>$checked,
		'createtime'=>time(),
	);
	pdo_insert('jy_quan_item_msg',$data);
	if($checked){
		exi('评论成功！');
	}else{
		exi('评论成功！请等待审核');
	}
}
if($_RQ['op']=='like'){
	$id = $this->session('id');
	$postdata=get_postdata();
	if(!$id){
		exi('没有找到该帖子，请重新选择！','error');
	}
	$looklist=pdo_get('jy_quan_item_like',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'pid'=>$id));
	if($looklist){
		if($looklist['islike'] !=1){
			pdo_update('jy_quan_item_like',array('islike'=>1),array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'pid'=>$id));
		}elseif($postdata['like'] !=1){
			pdo_update('jy_quan_item_like',array('islike'=>0),array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'pid'=>$id));
		}
	}else{
		pdo_insert('jy_quan_item_like',array('uniacid'=>$_SESSION['uniacid'],'pid'=>$id,'islike'=>1,'fromuser'=>$this->session('fromuser'),'mid'=>$_SESSION['uid']));
	}
	exi('点赞成功');
}
if($_RQ['op']=='msglike'){
	$id = $this->session('id');
	$postdata=get_postdata();
	$cid = intval($postdata['cid']);
	if(!$id){
		exi('没有找到该帖子，请重新选择！','error');
	}
	$params = array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'pid'=>$id,'cid'=>$cid);
	$msglike=pdo_get('jy_quan_item_msglike',$params);
	if($msglike){
		if($msglike['islike'] !=1){
			pdo_update('jy_quan_item_msglike',array('islike'=>1),$params);
			
		}elseif($postdata['like'] !=1){
			pdo_update('jy_quan_item_msglike',array('islike'=>0),$params);
		}
	}else{
		$data=array(
			'mid'=>$_SESSION['uid'],
			'pid'=>$id,
			'cid'=>$cid,
			'uniacid'=>$_SESSION['uniacid'],
			'islike'=>1,
		);
		pdo_insert('jy_quan_item_msglike',$data);
	}
	exi('点赞成功');
}
if($_RQ['op']=='readjieli'){
	$id = $this->session('id');
	$item=pdo_get('jy_quan_item',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	if(!$id){
		exi('没有找到该帖子，请重新选择！','error');
	}
	$insert=array(
				'uniacid'=>$_SESSION['uniacid'],
				'mid'=>$_SESSION['uid'],
				'pid'=>$id,
				'snsid'=>$item['snsid'],
				'type'=>$item['type'],
				'orderid'=>0,
				'createtime'=>TIMESTAMP
	);
	pdo_insert('jy_quan_item_jieli',$insert);
	exi('接龙成功');
}
if($_RQ['op']=='setfileds'){
	$id = $this->session('id');
	$fileds = json_decode($this->session('fileds'),true);
	if(empty($fileds)){
		$item=pdo_get('jy_quan_item',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
		$fileds = json_decode($item['fileds'],true);
		foreach($fileds as &$pv){
			$pv['total']=0;
		}
	}
	$postdata=get_postdata();
	$type = intval($postdata['type']);
	$index = intval($postdata['index']);
	if($type){
		$fileds[$index]['total'] +=1;
	}else{
		$fileds[$index]['total'] -=1;
	}
	$fileds = ijson_encode($fileds);
	$this->session('fileds',$fileds);
	exi('成功！');
}
if($_RQ['op']=='setfileds_optionid'){
	$id = $this->session('id');
	$fileds = json_decode($this->session('fileds'),true);
	if(empty($fileds)){
		$item=pdo_get('jy_quan_item',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
		$fileds = json_decode($item['fileds'],true);
		foreach($fileds as &$pv){
			$pv['total']=0;
		}
	}
	$postdata=get_postdata();
	$optionid = intval($postdata['optionid']);
	$index = intval($postdata['index']);
	$fileds[$index]['optionid']=$optionid;
	$fileds = ijson_encode($fileds);
	$this->session('fileds',$fileds);
	exi('成功！');
}
if($_RQ['op']=='sendhb'){
	$id=$_RQ['id'];
	if(!$_RQ['id']){
		$id=$this->session('id');
	}
	if(!$id){
		exi('未正确获取作品id','error');
	}
	
	$post=array();
	$post['money']=0;
	 $user=pdo_get('jy_quan_member',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']));
	 $item=pdo_get('jy_quan_item',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	if($_RQ['postdata']){
	   $postdata=get_postdata();
	   $money=$postdata['money'];
	   $username=$postdata['username'];
	   if($money<=0){
		   exi('红包金额需大于0元','error');
	   }
	   if($money>200){
		   exi('单个红包金额最多不超过200元','error');
	   }
	   if(!$username){
		   exi('请输入发送人姓名！','error');
	   }
	   //创建红包订单
	   $insert=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$_SESSION['uid'],
			'pid'=>$id,
			'omid'=>$item['mid'],
			'fee'=>$money,
			'username'=>$username,
			'ispay'=>0,
			'status'=>0,
			'createtime'=>TIMESTAMP
	   );
	   pdo_insert('jy_quan_redorder',$insert);
	   //支付
	   $id=pdo_insertid();
	   $params['fee']=$money;
	    $params['title']='发送红包订单';
		$params['id']=$id;
		$params['plugin']='jy_quan';
		$params['back_url']='jy_quan/o_detail/payresult';
		cfc('core')->pay($params);
	}
	$return['post']=$post;
	exi($return);
}

if($_RQ['op']=='payresult'){
	$payresult=$_SESSION['payresult'];unset($_SESSION['payresult']);
	if(!$payresult){
		exi('支付失败，未收到支付结果！','error');
	}
	if($payresult['status']!=1){
		exi('支付失败！','error');
	}
	$order=pdo_get('jy_quan_redlog',array('id'=>$payresult['id'],'uniacid'=>$_SESSION['uniacid']));
	if(!$order){
		exi('支付失败！发送红包订单不存在！','error');
	}
	if($order['fee']!=$payresult['fee']){
		exi('支付失败！发送红包订单金额不匹配！','error');
	}
	pdo_update('jy_quan_redorder',array('ispay'=>1,'fee'=>$payresult['fee']),array('id'=>$order['id']));
	//发红包
	//收红包人的信息
	$suser=pdo_get('core_fans',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$order['omid']));
	$arr['openid'] = $suser['openid'];
	$arr['hbname'] = '场景投票';
	$arr['body'] = "场景投票红包";
	$arr['fee'] = $order['fee'];
	$arr['openid'] = $fans['openid'];
	$arr['mchid'] = $basic['mchid'];
	$arr['mch_appid'] = $basic['mch_appid'];
	$arr['username'] = $order['username'];
	$res = sendhongbaoto($arr);
 	if($res['result_code'] == 'SUCCESS'){
		pdo_update('jy_quan_redorder',array('status'=>1),array('id'=>$order['id']));
		exi('支付成功','success');
	}
	else{
		exi('红包发送失败','error');
	}
}
if ($_RQ['op'] == 'pregetred') {
	$resultJson = [];
  	$postdata = get_postdata();
	$pid = intval($postdata['pid']);
	$isjielong=pdo_getcolumn('jy_quan_item_jieli',array('pid'=>$pid,'uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']),'id');
	if(!$isjielong){
		exi('请先接龙，才能抢红包噢！','error');
	}
	$isgetred=pdo_getcolumn('jy_quan_redlog',array('pid'=>$pid,'uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'status'=>1),'id');
	if($isgetred){
		exi('您已抢过该红包！','error');
	}
	$item=pdo_get('jy_quan_item',array('id'=>$pid,'uniacid'=>$_SESSION['uniacid']),array('title','desc','mid','rednum','snsid','type'));
	$red=pdo_get('jy_quan_redlog',array('pid'=>$pid,'uniacid'=>$_SESSION['uniacid'],'mid'=>0,'status'=>1),array('fee','setmid'));
  
  	$sender = pdo_get('core_members',array('id'=>$red['setmid'],'uniacid'=>$_SESSION['uniacid']), array('nickname', 'avatar'));
	$member = pdo_get('jy_quan_member',array('mid'=>$red['setmid'],'uniacid'=>$_SESSION['uniacid']),array('title', 'logo'));
  
  	$send_name = $member['title'];
	$nickname  = $sender['nickname'];
  	$send_name = $send_name?$send_name:$nickname;
  	
  	$send_avatar = $member['logo'] ? $member['logo'] : $sender['avatar'];
  	$send_avatar = $send_avatar ? $send_avatar :  '../core/resource/images/nopic.jpg';
	
	$issendred=pdo_getsum('jy_quan_redlog',array('pid'=>$pid,'uniacid'=>$_SESSION['uniacid'],'mid <>'=>0,'status'=>1),'fee');
	$issendnum=pdo_count('jy_quan_redlog',array('pid'=>$pid,'uniacid'=>$_SESSION['uniacid'],'mid <>'=>0,'status'=>1),'id');
	$yufee=$red['fee']-$issendred;
	$yutotal=$item['rednum']-$issendnum;
	if($yutotal<1 && $yufee<=0){
		exi('噢！红包已抢完，下次再来吧！','error');
	}
  
  	$resultJson = array_merge($resultJson, [
            'red_sender_name' => $send_name,
            'red_sender_avatar' => $send_avatar,
            'red_title' => $item['title']
            ]);
    exi($resultJson);
}

